/**
 * main.js
 */
(function(global, undefined) {

if (global.YL) {
  return
} else {
	global.YL = {}
}
/**
 * get ie version 6-9
 * https://gist.github.com/padolsey/527683
 */ 
var ie = (function(){
    var undef,
        v = 4,
        div = document.createElement('div'),
        all = div.getElementsByTagName('i');

    while (
        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
        all[0]
    );
    
    return v > 5 ? v : undef
}())

YL.ie = ie

;(function() {
	if (YL.ie && YL.ie < 9) {
		var root = document.documentElement
		var cls = root.className
		if (cls.indexOf('lt-ie9') === -1) {
			root.className = cls ? 'lt-ie9 ' + cls  : 'lt-ie9'
		}
	}
}())
})(this);
$(document).ready(function(){
	var $maina = $('div.main-a');
	if ($maina.length > 0) {
		var h = $('div.main').height();
		if ($maina.height() < h) {
			$maina.height(h);
		}
	}

	// navbar
	(function(){
		var nav = $('div.navbar');
		if (nav.length > 0) {
			var li = nav.find('li.active');
			// span.line margin
			var n = 5;
			if (li.is(":first-child")) {
				if (li.is(":only-child")) {
					n = -6;
				} else {
					n = 0;
				}
			}
			var left = li.position().left + (li.width()/2) + n;
			nav.children('div.ab').css('left', left);
		}
	})();

	// search input
	(function(){
		var labinp = $('.J_labinp');
		var label = labinp.find('label');
		var input = labinp.find('.input');

		function checkLabelInput() {
			if (!$.trim(input.val())) {
					input.val("");
					label.show();
			}
		}

		checkLabelInput();

		if (labinp.length > 0) {	
			label.click(function(){input.focus(); }); 
			input.focus(function(){label.hide(); }).blur(checkLabelInput); 
			labinp.on('click', '.btn', checkLabelInput);
		}
	})();

});
/**
 * 返回顶部
 *
 * 不考虑IE6，它下面隐藏
*/

function toTop(acceleration, time) {
	acceleration = acceleration || 0.1;
	time = time || 16;

	var y = document.documentElement.scrollTop;
	// 滚动距离 = 目前距离 / 速度。速度越大，新位置就越小，滚动越快。
	var speed = 1 + acceleration;
	window.scrollTo(0, Math.floor(y / speed));
	// 如果距离不为零, 继续调用迭代本函数。函数在全局
	if( y > 0) {
		var invokeFunction = "toTop(" + acceleration + ", " + time + ")";
		window.setTimeout(invokeFunction, time);
	} 
}

$(document).ready(function(){
	$('#totop').click(function(){
		toTop(0.5);
		return false;
	});
})


$(window).scroll(function(){
	var t = document.documentElement.scrollTop || document.body.scrollTop;
	t > 80 ? $('#totop').show() : $('#totop').hide();
});
